/*
 * Copyright (C) 2017 Pingan, Inc. All Rights Reserved.
 */

package com.pingan.haofang.searchcloud.web.index.vo.test;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

/**
 * RPC测试结果
 *
 * @author zhangbi617
 * @date 19/05/2017
 */
@ApiModel(description = "RPC测试结果")
public class RpcTestResponseVO {

    /**
     * 是否成功
     */
    @ApiModelProperty("是否成功，成功:true,失败:false")
    private boolean success;

    /**
     * 错误信息
     */
    @ApiModelProperty("错误信息")
    private String errorMessage;

    /**
     * 数据
     */
    @ApiModelProperty("数据")
    private Object data;

    public static RpcTestResponseVO ofSuccess(Object data) {
        RpcTestResponseVO vo = new RpcTestResponseVO();
        vo.setSuccess(true);
        vo.setData(data);
        return vo;
    }

    public static RpcTestResponseVO ofFailed(String errorMessage, Exception e) {
        RpcTestResponseVO vo = new RpcTestResponseVO();
        vo.setSuccess(false);

        StringBuilder throwableInfos = new StringBuilder(256);
        if (e != null) {
            ByteArrayOutputStream throwableOut = new ByteArrayOutputStream();
            PrintStream throwableStream = new PrintStream(throwableOut);
            e.printStackTrace(throwableStream);
            throwableInfos.append(new String(throwableOut.toByteArray()));
        }
        vo.setErrorMessage(errorMessage + "\n" + throwableInfos.toString());
        return vo;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "RpcTestResponseVO{" +
                "success=" + success +
                ", errorMessage='" + errorMessage + '\'' +
                ", data=" + data +
                '}';
    }
}
